<template>
	<view class="root-wrap">
		<view class="form-item">
			<view class="label">
				<text class="title">营业执照（系统自动识别基本信息）</text>
				<text v-if="!readOnly" class="require">*</text>
			</view>
			<view class="img-wrap">
				<u-upload
					:fileList="fileList1"
					@afterRead="afterRead"
					@delete="deletePic"
					:previewFullImage="true"
					name="1"
					multiple
					:maxCount="1"
				></u-upload>
			</view>
		</view>
		<view class="form-item">
			<view class="label">
				<text class="title">供应商名称</text>
				<text v-if="!readOnly" class="require">*</text>
			</view>
			<view class="input-wrap">
				<u-input
					placeholder="请输入供应商名称"
					border="none"
					v-model="userInfo.vendor_name"
					fontSize="28rpx"
					:readonly="readOnly"
					@change="e=>change(e,'vendor_name')"
				  ></u-input>
			</view>
		</view><view class="form-item">
			<view class="label">
				<text class="title">营业执照号</text>
				<text v-if="!readOnly" class="require">*</text>
			</view>
			<view class="input-wrap">
				<u-input
					placeholder="请输入营业执照号"
					border="none"
					v-model="userInfo.reg_num"
					fontSize="28rpx"
					:readonly="readOnly"
					@change="e=>change(e,'reg_num')"
				  ></u-input>
			</view>
		</view>
		<view class="form-item">
			<view class="label">
				<text class="title">法人代表</text>
				<text v-if="!readOnly" class="require">*</text>
			</view>
			<view class="input-wrap">
				<u-input
					placeholder="请输入法人代表"
					border="none"
					v-model="userInfo.faren"
					fontSize="28rpx"
					:readonly="readOnly"
					@change="e=>change(e,'faren')"
				  ></u-input>
			</view>
		</view>
		<view class="form-item">
			<view class="label">
				<text class="title">法人身份证号</text>
				<text v-if="!readOnly" class="require">*</text>
			</view>
			<view class="input-wrap">
				<u-input
					placeholder="请输入法人身份证号"
					border="none"
					v-model="userInfo.faren_card"
					fontSize="28rpx"
					:readonly="readOnly"
					@change="e=>change(e,'faren_card')"
				  ></u-input>
			</view>
		</view>
		
		<view class="form-item">
			<view class="label">
				<text class="title">企业性质</text>
				<text v-if="!readOnly" class="require">*</text>
			</view>
			<view class="input-wrap" @click="actionSheetShow=true">
				<u-input
					placeholder="请选择企业性质"
					border="none"
					disabled
					suffixIcon="arrow-down"
					v-model="userInfo.vendor_xingzhi_text"
					fontSize="28rpx"
					:readonly="readOnly"
					@change="e=>change(e,'vendor_xingzhi')"
				  ></u-input>
			</view>
		</view>
		<view class="form-item">
			<view class="label">
				<text class="title">注册资金（万元）</text>
				<text v-if="!readOnly" class="require">*</text>
			</view>
			<view class="input-wrap">
				<u-input
					placeholder="请输入注册资金"
					border="none"
					v-model="userInfo.zhuce_money"
					fontSize="28rpx"
					:readonly="readOnly"
					@change="e=>change(e,'zhuce_money')"
				  ></u-input>
			</view>
		</view>
		<view class="form-item">
			<view class="label">
				<text class="title">主营产品，主营业务介绍</text>
				<text v-if="!readOnly" class="require">*</text>
			</view>
			<view class="input-wrap">
				<u-input
					placeholder="请输入主营产品，主营业务介绍"
					border="none"
					v-model="userInfo.business_content"
					fontSize="28rpx"
					:readonly="readOnly"
					@change="e=>change(e,'business_content')"
				  ></u-input>
			</view>
		</view>
		<view class="form-item">
			<view class="label">
				<text class="title">企业地址</text>
				<text v-if="!readOnly" class="require">*</text>
			</view>
			<view class="input-wrap">
				<u-input
					placeholder="企业地址"
					border="none"
					v-model="userInfo.address"
					fontSize="28rpx"
					:readonly="readOnly"
					@change="e=>change(e,'address')"
				  ></u-input>
			</view>
		</view>
		<view class="form-item">
			<view class="label">
				<text class="title">企业简介</text>
				<text v-if="!readOnly" class="require">*</text>
			</view>
			<view class="input-wrap">
				<u-input
					placeholder="请输入企业简介"
					border="none"
					v-model="userInfo.intro_content"
					fontSize="28rpx"
					:readonly="readOnly"
					@change="e=>change(e,'intro_content')"
				  ></u-input>
			</view>
		</view>
		<view class="form-item">
			<view class="label">
				<text class="title">企业网址</text>
			</view>
			<view class="input-wrap">
				<u-input
					placeholder="请输入企业网址"
					border="none"
					v-model="userInfo.url"
					fontSize="28rpx"
					:readonly="readOnly"
					@change="e=>change(e,'url')"
				  ></u-input>
			</view>
		</view>
		<view class="submit"><u-button @click="onSubmit" color="#30D6C2" type="primary" shape="circle">保存<image class="icon" src="../../../static/assets/imgs/next-step.png"></image></u-button></view>
		<u-action-sheet :actions="actionSheetList" :show="actionSheetShow" @select="actionSheetCallback"
					description="请选择企业性质" @close="actionSheetShow = false">
				</u-action-sheet>
	</view>
</template>

<script>
	import config from '@/common/config.js';
	import { updateBaseInfo,getUserInfo,getMiscInfo } from '@/api/user.js';
	export default {
		data() {
			return {
				userInfo: {},
				readOnly: false,
				fileList1: [],
				loading: false,
				actionSheetList:[],
				actionSheetShow: false,
			};
		},
		onLoad(){
			this.userInfo = this.vuex_user;
			this.initSelect();
			this.getUserInfo();
		},
		methods:{
			initSelect(){
				getMiscInfo().then((res)=>{
					let obj = res.data.xz;
					Object.keys(obj).forEach(key => {
					  this.actionSheetList.push({
						  name: obj[key],
						  value: key
					  })
					});
				})
			},
			getUserInfo(){
				getUserInfo().then((res)=>{
					if (1 == res.code) {
						this.userInfo = res.data;
						if(res.data.license_image_url!=''){
							this.fileList1 = [{
								url: res.data.license_image_url
							}]
						}
						if(res.data.vendor_xingzhi){
							this.userInfo.vendor_xingzhi_text = res.data.vendor_xingzhi == 1?'国有企业':(res.data.vendor_xingzhi == 2?'私营企业':(res.data.vendor_xingzhi == 3?'外资企业':'个体工商户'));
						}
						this.$u.vuex('vuex_user', res.data)
					} else {
						uni.showToast({
							title:res.msg,
							icon:'none'
						})
					}
				})
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url);
					console.log(result)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				let that = this;
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: config.baseUrl+'addons/bjjb/api.common/upload',
						filePath: url,
						name: 'file',
						formData: {
							'type':'license'
						},
						header: {
							'Token': that.vuex_token
						},
						success: (uploadFileRes) => {
							const res = JSON.parse(uploadFileRes.data)
							if(res.data.parseData){
								that.userInfo.vendor_name = res.data.parseData.vendor_name;
								that.userInfo.reg_num = res.data.parseData.reg_num;
								that.userInfo.faren = res.data.parseData.faren;
								that.userInfo.zhuce_money = res.data.parseData.zhuce_money;
								that.userInfo.business_content = res.data.parseData.business_content;
								that.userInfo.address = res.data.parseData.address;
							}
							setTimeout(() => {
								resolve(res.data.fullurl)
							}, 500)
						}
					});
				})
			},
			actionSheetCallback(e){
				this.userInfo.vendor_xingzhi = e.value;
				this.userInfo.vendor_xingzhi_text = e.name;
			},
			change(e,key){
				this.userInfo[key] = e;
			},
			formatUrl(fileList){
				let image_str = '';
				if(fileList.length>0){
					const url = fileList[0].url;
					image_str = url.replace(/^.*?(\/uploads.*)$/, '$1');
				}
				return image_str;
			},
			onSubmit(){
				if(this.loading) return;
				const { id,vendor_name,reg_num,faren,faren_card,zhuce_money,vendor_xingzhi,license_image,license_image_url,business_content,address,intro_content,url } = this.userInfo;
				if(!this.fileList1[0].url){
					uni.showToast({
						title:'请上传营业执照',
						icon:'none'
					})
					return;
				}
				
				if(!vendor_name || !reg_num || !faren || !faren_card || !zhuce_money || !vendor_xingzhi || !business_content || !address || !intro_content){
					uni.showToast({
						title:'请检查所有必填项',
						icon:'none'
					})
					return;
				}
				this.loading = true;
				uni.showLoading({
					title: '提交中...'
				})
				const params = {
					"vendor_id": id, //供应商ID
					"license_image": this.formatUrl(this.fileList1), //营业执照
					"vendor_name": vendor_name, //供应商名称
					"reg_num": reg_num, //供应商名称
					"faren": faren, //法人
					"faren_card": faren_card, //法人身份证
					"zhuce_money": zhuce_money, //注册资金
					"vendor_xingzhi": vendor_xingzhi, //企业性质
					"business_content": business_content, //主营业务介绍
					"address": address, //企业地址
					"intro_content": intro_content, //企业简介
					"url": url //企业网址
				}
				updateBaseInfo(params).then(res => {
					this.loading = false;
					uni.hideLoading();
					if(res.code==1){
						uni.showToast({
							title:'提交成功',
							icon:'success'
						})
						const timer = setTimeout(()=>{
							uni.navigateBack({
								delta: 1
							})
							clearTimeout(timer);
						},900);
					}else {
						uni.showToast({
							title:res.msg,
							icon:'none'
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss">
.root-wrap{
	padding-bottom: 110rpx;
	.form-item{
		margin: 0 24rpx;
		.label{
			display: flex;
			justify-content: flex-start;
			align-items: center;
			margin: 44rpx 0 24rpx;
			.title{
				font-size: 28rpx;
				color: #606266;
				line-height: 50rpx;
			}
			.require{
				font-size: 28rpx;
				color: #E62020;
				line-height: 50rpx;
				margin-left: 24rpx;
			}
		}
		.input-wrap{
			width: 700rpx;
			height: 90rpx;
			background-color: #F3F4F6;
			margin: 0 auto;
			border-radius: 8rpx;
			.u-input{
				height: 100% !important;
				input{
					padding: 0 24rpx !important;
				}
			}
		}
	}
	.submit{
		width: 208rpx;
		height: 80rpx;
		margin: 88rpx auto 110rpx;
		.icon{
			width: 48rpx;
			height: 48rpx;
			margin-left: 16rpx;
		}
		.label{
			font-weight: 500;
			font-size: 32rpx;
			color: #fff;
		}
	}
	.u-input__content__subfix-icon{
		margin-right: 16rpx;
	}
}
</style>
